<?

class LoggerUAParser
{
    protected static $operable;
    protected static $uaIni;
    protected static $cache;

    public function __construct() 
    {
        if (!is_array(self::$cache))
            self::$cache = array(
                'robot' => array(),
                'browser' => array(),
                'platform' => array()
            );

        $uaIniFile = dirname(__FILE__) . '/ua_parser.ini';
        $uaIniUrl = 'http://user-agent-string.info/rpc/get_data.php?key=free&format=ini';

        if (!isset(self::$uaIni))
        {
            self::$uaIni = array(
                'robots' => array(),
                'browser_reg' => array(),
                'browser' => array(),
                'os_reg' => array(),
                'os' => array()
            );
            self::$operable = false;
            if (file_exists($uaIniFile))
            {
                if (LoggerConfig::$updateExtDatabases && is_writable($uaIniFile))
                {
                    $fileMTime = @filemtime($uaIniFile);
                    if ($fileMTime !== false && $fileMTime < (time() - 5*24*3600))
                    {
                        $newContent = @file_get_contents($uaIniUrl);
                        if ($newContent !== false)
                        {
                            $fp = @fopen($uaIniFile, 'w');
                            if ($fp)
                            {
                                fwrite($fp, $newContent);
                                fclose($fp);
                            }
                        }
                    }
                }
                $parseResult = parse_ini_file($uaIniFile, true);
                if ($parseResult !== false)
                {
                    self::$uaIni = $parseResult;
                    self::$operable = true;
                }
            }
        }
    }

    public function operable()
    {
        return self::$operable;
    }

    public function getRobot($agent)
    {
        if (!self::$operable)
            return false;

        if (isset(self::$cache['robot'][$agent]))
            return self::$cache['robot'][$agent];

        foreach (self::$uaIni['robots'] as $test)
        {
            if ($test[0] == $agent)
            {
                self::$cache['robot'][$agent] = $test[1];
                return $test[1];
            }
        }
        return false;
    }

    public function getBrowser($agent)
    {
        if (!self::$operable)
            return false;

        if (isset(self::$cache['browser'][$agent]))
            return self::$cache['browser'][$agent];

        foreach (self::$uaIni['browser_reg'] as $test)
        {
            if (preg_match($test[0], $agent, $match))
            {
                $browserId = $test[1];
                $browserVer = '';
                if (count($match) > 1)
                    $browserVer = $match[1];
                if (isset(self::$uaIni['browser'][$browserId]))
                {    
                    $result = array(
                        'name' => self::$uaIni['browser'][$browserId][1],
                        'ver' => $browserVer
                    );
                    self::$cache['browser'][$agent] = $result;
                    return $result;
                }
                return false;
            }
        }
        return false;
    }

    public function getPlatform($agent)
    {
        if (!self::$operable)
            return false;

        if (isset(self::$cache['platform'][$agent]))
            return self::$cache['platform'][$agent];

        foreach (self::$uaIni['os_reg'] as $test)
        {
            if (preg_match($test[0], $agent))
            {
                $osId = $test[1];
                if (isset(self::$uaIni['os'][$osId]))
                {
                    $result = array(
                        'family' => self::$uaIni['os'][$osId][0],
                        'name' => self::$uaIni['os'][$osId][1]
                    );
                    self::$cache['platform'][$agent] = $result;
                    return $result;
                }
                return false;
            }
        }
        return false;
    }
}